<?php

namespace app\controller;

use support\Request;
use app\model\BlogCategory;
use app\model\BlogArticle;

class CategoryController
{
    /**
     * 某个分类下的文章列表
     */
    public function index(Request $request, string $cate_name)
    {
        if (!$cate_name) {
            return response('错误的请求!', 400);
        }
        $cates = BlogCategory::withCount('articles')->get()->toArray();
        $articles = BlogArticle::where('cate_name', $cate_name)->orderByDesc('created_at')->get()->toArray();
        return view('category/index', ['cates' => $cates, 'cate_name' => $cate_name, 'articles' => $articles]);
    }
}